/**
 * 提交查询
 * @return
 */
function searchItemSubmit() {
	var spara = $("#itemListFrm").serialize();
	doActionNoCallback("core", $("#itemListFrm").get(0).action, spara);
}

/**
 * 编辑商品编码
 * @return
 */
function editItemCodeSubmit() {
	var spara = $("#editItemCodeFrm").serialize();
	doActionNoCallback("core", $("#editItemCodeFrm").get(0).action, spara);
}

/**
 * 编辑商品详情
 * @return
 */
function editItemDetailSubmit() {
//	设置图片地址上传
	var itemImageList = $("#itemImages img");
	var imageListLenth = itemImageList.length;
	var descImage = "";
	if (imageListLenth >= 0) {
		$.each(itemImageList,function (index,imageEle){
			if(index == 0) {
				$("#picInput").val(imageEle.src);
			} else {
				descImage = descImage + imageEle.src + ",";
			}
		});
		if (descImage.length > 0) {
			descImage = descImage.substring(0,descImage.length-1);
		}
		$("#descPicInput").val(descImage);
	}
	
	//设置商品编码图片
	var itemCodeImage = $("#itemCodeImage img");
	if(itemCodeImage.length && itemCodeImage.length>0) {
		$("#itemCodePicInput").val(itemCodeImage[0].src);
	}
	
	//设置tag列表值
	setTagListString();
		
	var spara = $("#editItemDetailFrm").serialize();
	doActionNoCallback("core", $("#editItemDetailFrm").get(0).action, spara);
}

/**
 * 发布商品详情
 * @return
 */
function publishItemSubmit() {
	var miniSkuList ="[";
	var skuInputList = $("input.propertyInput");
	$.each(skuInputList,function (index,inputEle){
		var propertyValueId = $(inputEle).attr('id').replace("propertyId","");
		var stock = inputEle.value;
		if (stock!=0) {
			miniSkuList = miniSkuList + "{\"ids\":\""+propertyValueId+"\",\"stock\":\""+stock+"\"},";
		}
	});
	if (miniSkuList.length > 1) {
		miniSkuList = miniSkuList.substring(0,miniSkuList.length-1);
	}
	miniSkuList = miniSkuList + "]";
	$("input.miniSkuListInput").val(miniSkuList);
	
//	设置图片地址上传
	var itemImageList = $("#itemImages img");
	var imageListLenth = itemImageList.length;
	var descImage = "";
	if (imageListLenth >= 0) {
		$.each(itemImageList,function (index,imageEle){
			if(index == 0) {
				$("#picInput").val(imageEle.src);
			} else {
				descImage = descImage + imageEle.src + ",";
			}
		});
		if (descImage.length > 0) {
			descImage = descImage.substring(0,descImage.length-1);
		}
		$("#descPicInput").val(descImage);
	}
	
//设置商品编码图片
	var itemCodeImage = $("#itemCodeImage img");
	if(itemCodeImage.length && itemCodeImage.length>0) {
		$("#itemCodePicInput").val(itemCodeImage[0].src);
	}
	
	//设置tag列表值
	setTagListString();
	
	var spara = $("#publishItemFrm").serialize();
	doActionNoCallback("core", $("#publishItemFrm").get(0).action, spara);
}

/**
 * 选择类目的时候，查询品牌和尺寸，设置标签
 * @return
 */
function selectCategory(category) {
	
	var categoryId = $(category).val();
	//设置品牌
	$("#brandSelect").empty();
	if (categoryId < 0) {
    	$("#brandSelect").append("<option value='-1' selected ='selected'>--请先选择类目--</option>");
    	return;
    }
	$.ajax({
        type: "GET",
        url: appServer + "/back/brand/getBrandListByForestId.htm?forestId=" + categoryId,
        dataType: "json",
        success: function(data){
        	if (data.success) {
            	$("#brandSelect").append("<option value='-1' selected ='selected'>--请选择品牌--</option>");
                $.each(data.data, function(brandIndex, brand){
                	$("#brandSelect").append("<option value='"+brand.brandId+"'>"+brand.brandTitle+"</option>"); 
              });
        	}
         },error: function (data, status, e){
        	alert(e);
        } 
    });
	//设置尺寸列表
	$.ajax({
        type: "GET",
        url: appServer + "/back/forestproperty/getPropertiesForForest.htm?pageSize=100&forestId=" + categoryId,
        dataType: "json",
        success: function(data){
        	if (data.success) {
        		$("tr.propertyClass").remove();
                $.each(data.data, function(forestPropertyValueIndex, forestPropertyValue){
                	$.each(forestPropertyValue.propertyValueDOList, function(propertyValueIndex, propertyValue){
                		var trHTML = "<tr class='propertyClass'><td>尺码：" + propertyValue.value
                		+ "&nbsp;&nbsp;&nbsp;库存：<input type='text' class='propertyInput' id='propertyId"
                		+ propertyValue.id +"' value='0'/></td></tr>";
                		$("#publishItemTbl tr:last").before(trHTML);
                    });
                });
        	}
         },error: function (data, status, e){
        	alert(e);
        } 
    });
	
	//设置类目为标签
	var selectedCategoryText = $(category).find("option:selected").text();
	$("span.categoryTags").remove();
	var selectedCategoryArray = selectedCategoryText.split('-');
	$.each(selectedCategoryArray,function (index,selectedCategory) {
		var tagHtml="<span class='categoryTags' style='position: relative;'>"
			+"<button class='tagListButton' type='button' disabled='disabled'>"+selectedCategory.replace(/(^\s*)|(\s*$)/g, "")+"</button>"
			+"<input type='image' style='width:15px;height:auto;' src='../../images/main/Delete.png' onclick='deleteTag(this);'>&nbsp;&nbsp;"
			+"</span>";
		$("#itemTags").append(tagHtml);
	});
}

/**
 * 选择品牌的时候，设置标签
 * @return
 */
function selectBrand(brand) {
	//设置品牌为标签
	var selectedBrandText = $(brand).find("option:selected").text();
	$("span.brandTag").remove();
	var tagHtml="<span class='brandTag' style='position: relative;'>"
		+"<button class='tagListButton' type='button' disabled='disabled'>"+selectedBrandText.replace(/(^\s*)|(\s*$)/g, "")+"</button>"
		+"<input type='image' style='width:15px;height:auto;' src='../../images/main/Delete.png' onclick='deleteTag(this);'>&nbsp;&nbsp;"
		+"</span>";
	$("#itemTags").append(tagHtml);
}

/**
 * 选择适用人群的时候，设置标签
 * @return
 */
function selectFeature(feature) {
	//设置适用人群为标签
	var selectedFeatureText = $(feature).find("option:selected").text();
	$("span.featureTag").remove();
	var tagHtml="<span class='featureTag' style='position: relative;'>"
		+"<button class='tagListButton' type='button' disabled='disabled'>"+selectedFeatureText.replace(/(^\s*)|(\s*$)/g, "")+"</button>"
		+"<input type='image' style='width:15px;height:auto;' src='../../images/main/Delete.png' onclick='deleteTag(this);'>&nbsp;&nbsp;"
		+"</span>";
	$("#itemTags").append(tagHtml);
}

function updataItemImage() {
	$.ajaxFileUpload({
		method:"POST",
	    url: appServer + "/api/image/uploadItemFile.htm",
	    secureuri:false,
	    fileElementId:'imageUpload',
        dataType: 'JSON',
        success: function(data){
        	data=data.substring(0,data.lastIndexOf('}')+1);
        	var result=JSON.parse(data);
        	var imgHtml="<span style='position: relative;'>"
        		+"<img src='"+result.data+"' class='itemImage' alt='商品图片' height='120px' width='auto'/>"
        		+"<input type='image' src='../../images/main/Delete.png' name='img'  onclick='deleteImage(this);' >&nbsp;&nbsp;"
        		+"</span>";
        	$("#itemImages").append(imgHtml);
      
        },error: function (data, status, e){
        	alert(data + e);
        } 
	});
	return false;
}

function updataItemCodeImage() {
	$.ajaxFileUpload({
		method:"POST",
	    url: appServer + "/api/image/uploadItemFile.htm",
	    secureuri:false,
	    fileElementId:'itemCodeImageUpload',
        dataType: 'JSON',
        success: function(data){
        	data=data.substring(0,data.lastIndexOf('}')+1);
        	var result=JSON.parse(data);
        	var imgHtml="<span style='position: relative;'>"
        		+"<img src='"+result.data+"' alt='商品编码图片' height='100px' width='auto'/>"
        		+"<input type='image' src='../../images/main/Delete.png' name='img'  onclick='deleteImage(this);' >&nbsp;&nbsp;"
        		+"</span>";
        	$("#itemCodeImage").html(imgHtml);
      
        },error: function (data, status, e){
        	alert(data + e);
        } 
	});
	return false;
}

function deleteImage(nowImage){
	$(nowImage).parent().remove();
}

function deleteTag(nowTag){
	$(nowTag).parent().remove();
}

function popUpdateTagsForItemDiv(id) {
	doActionNoPar("itemPopWindow", appServer + "/back/item/gotoUpdateTagsForItemPage.htm?itemId=" + id, function() {
		popupDiv("updateTagsForItemPopDiv");
	}, null);
}

function addItemTag() {
	var tagTitle = $("#tagInput").val();
	if (tagTitle != null && tagTitle.replace(/(^\s*)|(\s*$)/g, "").length > 0)  {
		var tagExists = false;
		var tagList = $("button.tagListButton");
		$.each(tagList,function (index,tagEle) {
			if ($(tagEle).text() == tagTitle) {
				tagExists = true;
				alert("标签“" + tagTitle +"”已存在");
				return false;
			}
		});
		if (!tagExists) {
			var tagHtml="<span style='position: relative;'>"
				+"<button class='tagListButton' type='button' disabled='disabled'>"+tagTitle+"</button>"
				+"<input type='image' style='width:15px;height:auto;' src='../../images/main/Delete.png' onclick='deleteTag(this);'>&nbsp;&nbsp;"
				+"</span>";
			$("#itemTags").append(tagHtml);
		}
	}
	$("#tagInput").val("");
}

function updateTagsForItemSubmit(itemId) {
	//设置tag列表值
	var tagListString = setTagListString();
	$.ajax({
	    type: "POST",
	    url: appServer + "/back/item/updateTagsForItem.htm",
	    data : {"itemId": itemId, "tagListString": tagListString},
	    dataType: "json",
	    success: function(result) {
	       if (result.success) {
	    	   $("#updateTagsForItemMsg").html("<h2>商品标签更新成功</h2>");
	       } else {
	    	   alert(result.errorMsg)
	       }
	    }
	});
}

function setTagListString() {
	var tagListString ="";
	var tagList = $("button.tagListButton");
	$.each(tagList,function (index,tagEle) {
		tagListString = tagListString + $(tagEle).text() +",";
	});
	if (tagListString.length > 1) {
		tagListString = tagListString.substring(0,tagListString.length-1);
	}
	$("input.tagListInput").val(tagListString);
	return tagListString;
}